//============================================================================
// Name        : GSP295_Wk2_MergeSort.cpp
// Author      : Ronald Ram
// Version     :
// Copyright   : GSP295 Wk2 Program - MergeSort
// Description : Merge Sort in C++
//============================================================================

#include <iostream>
#include "DoublyLinkedList.h";

using namespace std;
using namespace GSP295_Wk2_MergeSort;



void WelcomeMsg();
void Instructions();
void Command(char input);
void AddToList();
void SortList();
void ClearList();
void PrintList();



bool quit = false, bAddClearPrintQuit = false;
char yesNo, addClearPrintQuit;
string input, output;


DoublyLinkedList *list;


int main()
{
	list = new DoublyLinkedList();
	WelcomeMsg();

	do 
	{
		Instructions();
		cout << "Enter Command: "; cin >> addClearPrintQuit;

		Command(addClearPrintQuit);


	} while ( !quit );

	delete list;

	cout << "\n\n....Good Bye\n" << endl;
	system("pause");
	return 0;

}



//Decide on which command to trigger
void Command(char input)
{
	if ( input == 'A' || input == 'a' ) { AddToList();
	} 
	else if ( input == 'S' || input == 's' ) { SortList();
	}
	else if ( input == 'P' || input == 'p' ) { PrintList();
	}
	else if ( input == 'C' || input == 'c' ) { ClearList();
	}
	else if ( input == 'Q' || input == 'q' ) { quit = true;
	}
}


//Add string to List 
void AddToList()
{
	cout << "\nEnter a string: "; cin >> input;
	list->Clear();
	list->Add(input);

	cout << "\n\n======================================\n" << endl;
	cout << ">>> '" << input << "' - Successfully added to List <<<" << endl;
	cout << "\n\n======================================\n\n\n\n" << endl;
}

//Merge Sort string in List
void SortList()
{
	if ( list->isEmpty() ) 
	{
		cout << "\n\n======================================" << endl;
		cout << " >>>> RESULTS: <<<<" << endl; 
		cout << "======================================\n" << endl;
		cout << ">>> List is Empty, Nothing to Sort <<<" << endl;
		cout << "\n\n======================================\n\n\n\n" << endl;
	} 
	else 
	{
		list->MergeSort();

		cout << "\n\n======================================" << endl;
		cout << " >>>> RESULTS: <<<<" << endl; 
		cout << "======================================\n" << endl;
		cout << ">>> Successfully 'Merge Sorted' the List <<<" << endl;
		
		PrintList();
	}
}


//Print the sorted data in List
void PrintList() 
{

	if ( list->isEmpty() ) 
	{
		cout << "\n\n======================================\n" << endl;
		cout << "Results as entered. \nEnter 'Sort' command to sort list" << endl << endl;
	} 
	else 
	{
		cout << "\n\n======================================\n" << endl;
		cout << "Input Text: " << input << endl << endl;
	}
	list->Print();
}


//Delete all nodes in List
void ClearList()
{
	cout << "\n\n======================================" << endl;
	cout << " >>>> RESULTS: <<<<" << endl; 
	cout << "======================================\n" << endl;
	if ( !list->isEmpty() ) 
	{ 
		list->Clear();
		cout << ">>> Result: All nodes from List deleted <<<\n\n\n" << endl;
	} 
	else { cout << ">>> Result: List is Empty, nothing to delete <<<" << endl; }

	cout << "\n\n======================================\n\n\n\n" << endl;
}


//Welcome Message
void WelcomeMsg()
{
	cout << "\n**************************************************" << endl;
	cout << "\t GSP295 Week 2: iLab Assignments" << endl;
	cout << "\n\t Title: MergeSort - Program 2" << endl;
	cout << "\n\t Author: Ronald Ram" << endl;
	cout << "**************************************************\n\n\n" << endl;
}


//Instructions for Command keys to use
void Instructions()
{
	cout << "---------------------------------------" << endl;
	cout << "Command Options: " << endl;
	cout << "---------------------------------------" << endl;
	cout << "Add[A] - Sort[S] - Print[P] - Clear[C]" << endl;
	cout << "Q -> Quit" << endl;
	cout << "---------------------------------------\n" << endl;
}

